#include "stdafx.h"
#include "CListBox.h"


CListBox::CListBox()
	: m_iHovingIndex(-1)
{
	m_edge.Set(2, 2, 2, 2);
	m_sclVertical.SetScrollHost(this);
}
CListBox::CListBox(int_32 iX, int_32 iY, int_32 iWidth, int_32 iHeight)
	: CListControl(iX, iY, iWidth, iHeight), m_iHovingIndex(-1)
{
	m_edge.Set(2, 2, 2, 2);
	m_sclVertical.SetScrollHost(this);
}

CListBox::~CListBox()
{

}
const COID & CListBox::GetOID() const
{
	return OID_LISTBOX;
}
void CListBox::MouseMove(CMouseArgument & args)
{
	int_32 iOffX = GetClientSize().Width - m_sclVertical.GetScrollSize().Width;
	if(m_sclVertical.IsDraging())
	{
		args.m_ptMouse.X -= iOffX;
		m_sclVertical.ScrollMouseMove(args);
	}
	else if(args.m_ptMouse.X >= iOffX)
	{
		args.m_ptMouse.X -= iOffX;
		m_sclVertical.ScrollMouseMove(args);
	}
	else
	{
		CRectInt32 rcItem;
		int iHovingIndex = -1;	CRectInt32 rcClient = GetClient();
		int_32 iY = 0;
		int_32 iHightLightWidth = 0;
		for(int_x cnt = 0, size = m_arrItems.GetLength(); cnt != size; ++cnt)
		{
			const CListItem & item = m_arrItems[cnt];

			switch(m_eHightLightType)
			{
			case ListControlHightLightTypeItem:
				iHightLightWidth = item.Width;
				break;
			case ListControlHightLightTypeLine:
				iHightLightWidth = rcClient.Width;
				break;
			default:
				iHightLightWidth = item.Width;
				break;
			}

			rcItem.Width = iHightLightWidth;
			rcItem.Height = item.Height;

			if(rcItem.IsPointIn(args.m_ptMouse))
			{
				iHovingIndex = cnt;
				break;
			}
			rcItem.Y += item.Height;
		}
		SetHovingIndex(iHovingIndex);
	}
	CListControl::MouseMove(args);
}

void CListBox::MouseOut(CMouseArgument & args)
{
	SetHovingIndex(-1);
	CListControl::MouseOut(args);
}

void CListBox::MouseClick(CMouseArgument & args)
{
	SetSelect(m_iHovingIndex);
	CListControl::MouseClick(args);
}

void CListBox::FontFormatChanged()
{
	PerformLayerout();
	Repaint();
	CListControl::FontFormatChanged();
}

void CListBox::ForeColorChanged()
{
	Repaint();
	CListControl::ForeColorChanged();
}
void CListBox::OnPaintBack(IPaint * pPaint, const CRectInt32 & rcClip) const
{
	CRectInt32 rcControl = GetSize();
	//pPaint->Clear(0xFF808080);
	pPaint->DrawRect(0, 0, rcControl.Width - 1, rcControl.Height - 1, 0xFF808080, 1.0f);
}

void CListBox::OnPaint(IPaint * pPaint, const CRectInt32 & rectClip) const
{
	CRectInt32 rcClient = GetClient();
	int_32 iY = 0;
	int_32 iHightLightWidth = 0;
	CSolidBrush brush(m_uiForeColor);
	for(int_x cnt = 0, size = m_arrItems.GetLength(); cnt != size; ++cnt)
	{
		const CListItem & item = m_arrItems[cnt];
		switch(m_eHightLightType)
		{
		case ListControlHightLightTypeItem:
			iHightLightWidth = item.Width;
			break;
		case ListControlHightLightTypeLine:
			iHightLightWidth = rcClient.Width;
			break;
		default:
			iHightLightWidth = item.Width;
			break;
		}

		if(item.IsSelected)
			pPaint->FillRect(0, iY, iHightLightWidth, item.Height, 0xFF0080FF);
		else if(cnt == m_iHovingIndex)
			pPaint->FillRect(0, iY, iHightLightWidth, item.Height, 0xFFFFD700);
		pPaint->DrawString(item.Text, item.Text.GetLength(), 0, iY, &brush);
		iY += item.Height;
	}

	if(m_imgVertical.pImage)
	{
		int_32 iOffX = GetClientSize().Width - m_sclVertical.GetScrollSize().Width;
		pPaint->OffOrign(iOffX, 0);
		m_sclVertical.OnPaintScroll(pPaint, m_imgVertical, CBaseAttr(), CMouseState());
		pPaint->OffOrign(-iOffX, 0);
	}
}

void CListBox::SelectItem(int iIndex, bool bSelect)
{
	Repaint();
}

void CListBox::SetHovingIndex(int_32 iHovingIndex)
{
	if(iHovingIndex != m_iHovingIndex)
	{
		m_iHovingIndex = iHovingIndex;
		Repaint();
	}
}

//void CListBox::MinValueChanged()
//{
//	Repaint();
//	CScrollBase::MinValueChanged();
//}
//void CListBox::MaxValueChanged()
//{
//	Repaint();
//	CScrollBase::MaxValueChanged();
//}
//void CListBox::ValueChanged()
//{
//	Repaint();
//	CScrollBase::ValueChanged();
//}
//void CListBox::LineValueChanged()
//{
//	Repaint();
//	CScrollBase::LineValueChanged();
//}
//void CListBox::PageValueChanged()
//{
//	Repaint();
//	CScrollBase::PageValueChanged();
//}
//void CListBox::ScrollPointChanged()
//{
//	Repaint();
//	CScrollBase::ScrollPointChanged();
//}
//
//CSizeInt32 CListBox::GetScrollHostClientSize() const
//{
//	CSizeInt32 siClient = GetClientSize();
//	return CSizeInt32(32, siClient.Height);
//}
//CImageState CListBox::GetScrollHostImageState() const
//{
//	return GetImageState();
//}
//CBaseAttr CListBox::GetScrollHostBaseAttr() const
//{
//
//}
//CMouseState CListBox::GetScrollHostMouseState() const
//{
//
//}
//} 


void CListBox::SetVerticalScroll(IImage * pImage, int_32 iWidth, ImageMode eImageMode, ImageTileMode eImageTileMode)
{
	m_imgVertical.pImage = pImage;
	m_imgVertical.eImageMode = eImageMode;
	m_imgVertical.eImageTileMode = eImageTileMode;
	m_sclVertical.SetScrollSize(CSizeInt32(iWidth, GetSize().Height));
}

void CListBox::RepaintScroll()
{
	Repaint();
}